package com.king.cms.touch.action.admin;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.InstantiationException;

import com.king.cms.action.BaseDispatchAction;
import com.king.cms.common.Constants;
import com.king.cms.common.FileRw;
import com.king.cms.db.DBOperator;
import com.king.cms.touch.PageTurnUtil;

public class StaticContentEditAction extends BaseDispatchAction{
	private static final Log  log = LogFactory.getLog(StaticContentEditAction.class);
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws java.lang.InstantiationException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		 String action = request.getParameter("action");
		 String module= request.getParameter("module");
		 String contentId = request.getParameter("contentId");
		 request.setAttribute("module", module);
		 DBOperator dbo = this.getDBOperator(request);
		 if("add".equals(action)){ 
			 List menuList = dbo.select("select * from cms_touch_module_menu where MODULE_ID=? ORDER BY ORDER_NUM", new Object[]{module});
			 String menuStr = PageTurnUtil.selectMenu(menuList, null, "width:95%");
			 
			 request.setAttribute("menu", menuStr);
			 request.setAttribute("module", module);
			 request.setAttribute("content", "");
			 return mapping.findForward("contentview");
		 }
		 if("modify".equals(action)){ 
			 List menuList = dbo.select("select * from cms_touch_module_menu where MODULE_ID=? ORDER BY ORDER_NUM", new Object[]{module});
			 List contentList = dbo.select("select * from cms_touch_content where ID=?", new Object[]{contentId});
			  
			 String menuSelect = "";
			 String content = "";
			 if(contentList.size() > 0){
				 FileRw f = new FileRw();
				  Map mm = (Map)contentList.get(0);
				  menuSelect=mm.get("MENU_ID")+"";
				  //content = new String((byte[])mm.get("HTML_CONTENT"));
				  String filePath = request.getRealPath("/") + "touch\\html\\" + module + "_" + menuSelect + ".html";
				  content =f.ReadFile(filePath);
				  request.setAttribute("data", mm);
			 }
			 String menuStr = PageTurnUtil.selectMenu(menuList, menuSelect, "width:95%");
			 request.setAttribute("content", content);
			 request.setAttribute("contentId", contentId);
			 request.setAttribute("menu", menuStr);
			
			 return mapping.findForward("contentview");
		 }
		 if("save".equals(action)){
			 String sql = "";
			 boolean flag = false;
			 if(null == contentId || "null".equals(contentId) || "".equals(contentId)){
				 //sql = "insert into cms_touch_content(MODULE_ID,MENU_ID,CONTENT_NAME,CONTENT_DESC,HTML_URL,HTML_CONTENT) VALUES(?,?,?,?,?,?)";
				 sql = "insert into cms_touch_content(MODULE_ID,MENU_ID,CONTENT_NAME,CONTENT_DESC,HTML_URL) VALUES(?,?,?,?,?)";
			 }else{
				 flag = true;
				// sql = "update cms_touch_content set MODULE_ID=?,MENU_ID=?,CONTENT_NAME=?,CONTENT_DESC=?,HTML_URL=?,HTML_CONTENT=? WHERE ID = ?";
				 sql = "update cms_touch_content set MODULE_ID=?,MENU_ID=?,CONTENT_NAME=?,CONTENT_DESC=?,HTML_URL=? WHERE ID = ?";
				
			 }
			 String c_name = request.getParameter("contentName");
			 String c_menu = request.getParameter("menu");
			 String c_desc = request.getParameter("content_desc");
			 String c_content = request.getParameter("content");
			 request.setAttribute("contentId", contentId);
			 //log.info("submit content:" + request.getParameter("content"));
			 FileRw f = new FileRw();
			 String realPath = request.getRealPath("/") + "touch\\html\\" + module + "_" + c_menu + ".html";
			 log.info("static html path:" + realPath);
			 
			 f.writefilewithmqhxhtm(c_content, realPath);
			 byte[] contentByte = null;
			 if(null != c_content && !"null".equals(c_content))
				 contentByte = c_content.getBytes();
				 
			 if(flag)
				// dbo.update(sql, new Object[]{module,c_menu,c_desc,c_name,"touch\\html\\" + module + "_" + c_menu + ".html",contentByte,contentId});
			 	dbo.update(sql, new Object[]{module,c_menu,c_desc,c_name,"touch\\html\\" + module + "_" + c_menu + ".html",contentId});
			 else
				// dbo.update(sql, new Object[]{module,c_menu,c_desc,c_name,"touch\\html\\" + module + "_" + c_menu + ".html",contentByte}); 
				 dbo.update(sql, new Object[]{module,c_menu,c_desc,c_name,"touch\\html\\" + module + "_" + c_menu + ".html"});
			 request.setAttribute("content", request.getParameter("content"));
			 return new ActionForward("/staticList.do?module="+module);
		 }
		 if("del".equals(action)){ 
			 String[] cIds = request.getParameterValues("contentId");
			 if(null != cIds){
				 for(int i = 0;i<cIds.length;i++){
					 String id = cIds[i];
					 dbo.update("delete from cms_touch_content where id = ?", new Object[]{id});
				 }
			 }
			 request.setAttribute("contentId", contentId);
			 
			 return new ActionForward("/staticList.do?module="+module);
		 }
		 
		  
		 return null;
	}
	}
